import os
from Utility.Xml import *
import shutil

def make_define(dictionary_name,opt_index=0,learning_rate=1e-3,variables="LGA",sample_npz_name="Samples_clustered"):
    define={"dictionary_name":dictionary_name,
            "opt_index":opt_index,
            "learning_rate":learning_rate,
            "variables":variables,
            "sample_npz_name":sample_npz_name}
    return define


def get_control_params(define, need_clear_all=True):
    dictionary_name = define["dictionary_name"]
    opt_index=define["opt_index"]
    learning_rate = define["learning_rate"]
    variables = define["variables"]
    sample_npz_name = define["sample_npz_name"]

    dictionary_path = f"OPT/Dictionaries/{dictionary_name}.xml"
    # 获取根元素
    root = get_root_from_xml(dictionary_path)

    # 样本相关参数
    samples_path = get_str_from_xml(root, "in.samples.samples_path")

    # 结果文件夹目录
    result_folder_name =f"{dictionary_name}_IX{opt_index}_{variables}_LR{learning_rate}"
    result_path=f"{samples_path}/{result_folder_name}"

    if need_clear_all:
        # 清空 result_path 目录
        if os.path.exists(result_path):
            shutil.rmtree(result_path)
        os.makedirs(result_path, exist_ok=True)

    # dead模型相关参数
    dead_load_path = get_str_from_xml(root, "in.dead.dead_load_path")
    dead_data_num = get_int_from_xml(root, "in.dead.data_num")
    dead_hidden_size = get_int_from_xml(root, "in.dead.hidden_size")
    dead_depth = get_int_from_xml(root, "in.dead.depth")
    dead_latent_size = get_int_from_xml(root, "in.dead.latent_size")

    control_params = {
        "dictionary_name":dictionary_name,
        "samples_path": samples_path,
        "sample_npz_name": sample_npz_name,
        "opt_index": opt_index,
        "result_path": result_path,
        "iteration_num": get_int_from_xml(root, "in.opt.iteration_num"),
        # 优化项的初始学习率
        "learning_rate": learning_rate,
        "variables": variables,
        "enable_slot_term": get_int_from_xml(root, "in.opt.enable_slot_term"),
        "slot_min_web": get_double_from_xml(root, "in.opt.slot_min_web"),
        # 是否启用中心流道项
        "enable_port_term": get_int_from_xml(root, "in.opt.enable_port_term"),
        # 中心流道半径
        "central_port_radius": get_double_from_xml(root, "in.opt.central_port_radius"),
        # 中心流道位置因子（相对于 lz 的比例）
        "port_lz_factor": get_double_from_xml(root, "in.opt.port_lz_factor"),
        # eta项系数，取值0到100
        "factor_eta_term": get_double_from_xml(root, "in.opt.factor_eta_term"),
        # offset是用于防止优化后得到狭小的缝
        "offset_eta": get_double_from_xml(root, "in.opt.offset_eta")
    }

    dead_dic = {"dead_load_path": dead_load_path,
                "dead_data_num": dead_data_num,
                "dead_hidden_size": dead_hidden_size,
                "dead_depth": dead_depth,
                "dead_latent_size": dead_latent_size}

    return control_params, dead_dic

if __name__ == "__main__":
    define =make_define("dicA",1,1e-3,"LGA")
    control_params, dead_dic = get_control_params(define)
    print(control_params)
    print(dead_dic)